import threading
from testP import test44


# The philosopher thread
def philosopher(left, right):
    while True:
        # 获取到两把锁再执行
        with test44.acquire(left, right):
            print(threading.currentThread().getName(), 'eating')


# The chopsticks (represented by locks)
NSTICKS = 5
# 生成五个线程锁
chopsticks = [threading.Lock() for n in range(NSTICKS)]

# Create all of the philosophers
for n in range(NSTICKS):
    t = threading.Thread(target=philosopher,
                         args=(chopsticks[n], chopsticks[(n + 1) % NSTICKS]),
                         name='线程-' + str(n))
    t.start()
